package top.honlnk.czuan.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import top.honlnk.czuan.common.exception.CustomException;
import top.honlnk.czuan.common.result.Result;
import top.honlnk.czuan.pojo.entity.AiWordExplain;
import top.honlnk.czuan.service.AiService;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/ai")
public class AiController {

    @Autowired
    private AiService aiService;


    @GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> chatStream(String input) {
        if (input == null) {
            throw new CustomException("字典数据连接不可为空");
        }
        // TODO: 判断链接是否有效，链接中的数据是否合法。
        Flux<String> result = aiService.chatStream(input);
        return result
                .concatWith(Flux.just("[complete]"));
    }

    @GetMapping("/exist")
    public Result<List<AiWordExplain>> exist(String word) {
        if (word == null) {
            throw new CustomException("单词不可为空");
        }
        return Result.success(aiService.isExist(word));
    }
}
